/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.KDniao.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.KDniao.entity.LogisticsCode;
import com.thinkgem.jeesite.modules.KDniao.service.logistics.LogisticsCodeService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 物流公司信息Controller
 * @author Owen
 * @version 2017-04-05
 */
@Controller
@RequestMapping(value = "${adminPath}/logistics_code/logisticsCode")
public class LogisticsCodeController extends BaseController {

	@Autowired
	private LogisticsCodeService logisticsCodeService;
	
	@ModelAttribute
	public LogisticsCode get(@RequestParam(required=false) String id) {
		LogisticsCode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = logisticsCodeService.get(id);
		}
		if (entity == null){
			entity = new LogisticsCode();
		}
		return entity;
	}
	
	@RequiresPermissions("logistics_code:logisticsCode:view")
	@RequestMapping(value = {"list", ""})
	public String list(LogisticsCode logisticsCode, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LogisticsCode> page = logisticsCodeService.findPage(new Page<LogisticsCode>(request, response), logisticsCode); 
		model.addAttribute("page", page);
        return "modules/logistics/logisticsCodeList";
	}

	@RequiresPermissions("logistics_code:logisticsCode:view")
	@RequestMapping(value = "form")
	public String form(LogisticsCode logisticsCode, Model model) {
		model.addAttribute("logisticsCode", logisticsCode);
		return "modules/logistics/logisticsCodeForm";
	}

    @SuppressWarnings("unchecked")
    @RequiresPermissions("logistics_code:logisticsCode:edit")
	@RequestMapping(value = "save")
	public String save(LogisticsCode logisticsCode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, logisticsCode)){
			return form(logisticsCode, model);
		}
        User user = UserUtils.getUser();
        logisticsCode.setCreateUser(user.getName());
		logisticsCodeService.save(logisticsCode);
        List<LogisticsCode> logisticsCodes = (List<LogisticsCode>) CacheUtils.get("logisticsCodesisenadleList");
        if (logisticsCodes != null && logisticsCodes.size() > 0) {
            boolean fals = true;
            for (int i = 0; i < logisticsCodes.size(); i++) {
                LogisticsCode code=logisticsCodes.get(i);
                if (code.getId().equals(logisticsCode.getId())) {
                    logisticsCodes.remove(i);
                    logisticsCodes.add(logisticsCode);
                    fals = false;
                    break;
                }
            }
            if (fals) {
                logisticsCodes.add(logisticsCode);
            }
            CacheUtils.remove("logisticsCodesisenadleList");
            CacheUtils.put("logisticsCodesisenadleList", logisticsCodes);
        }
		addMessage(redirectAttributes, "保存物流公司信息成功");
        return "redirect:" + Global.getAdminPath() + "/logistics_code/logisticsCode/?repage";
	}
	
    @SuppressWarnings("unchecked")
    @RequiresPermissions("logistics_code:logisticsCode:edit")
	@RequestMapping(value = "delete")
    public String delete(LogisticsCode logisticsCode, RedirectAttributes redirectAttributes) {
		logisticsCodeService.delete(logisticsCode);
        List<LogisticsCode> logisticsCodes = (List<LogisticsCode>) CacheUtils.get("logisticsCodesisenadleList");
        if (logisticsCodes != null && logisticsCodes.size() > 0) {
            boolean fals = false;
            for (int i = 0; i < logisticsCodes.size(); i++) {
                LogisticsCode code = logisticsCodes.get(i);
                if (code.getId().equals(logisticsCode.getId())) {
                    logisticsCodes.remove(i);
                    fals = true;
                    break;
                }
            }
            if (fals) {
                CacheUtils.remove("logisticsCodesisenadleList");
                CacheUtils.put("logisticsCodesisenadleList", logisticsCodes);
            }
        }
		addMessage(redirectAttributes, "删除物流公司信息成功");
		return "redirect:"+Global.getAdminPath()+"/logistics_code/logisticsCode/?repage";
	}

}